package engine.util.vbo;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBVertexBufferObject;
import org.lwjgl.opengl.GLContext;

public class VBO {
	private VBO(){};
	
	public static int glGenBuffers() {
		if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
			IntBuffer buffers = BufferUtils.createIntBuffer(1);
			ARBVertexBufferObject.glGenBuffersARB(buffers);
			return buffers.get(0);
		}

		return -1;
	}
	
	public static void glGenBuffers(int[] buffers) {
		for(int i = 0; i < buffers.length; i++) {
			buffers[i] = glGenBuffers();
		}
	}

	public static void glBufferData(int ID, FloatBuffer data) {
		if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
			data.rewind();
			ARBVertexBufferObject.glBufferDataARB(
					ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, data,
					ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
		}
	}
	
	public static void glBufferElementData(int ID, IntBuffer data) {
		if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
			data.rewind();
			ARBVertexBufferObject.glBufferDataARB(
					ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, data,
					ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
		}
	}
	
	public static void glBufferElementData(int ID, ShortBuffer data) {
		if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
			data.rewind();
			ARBVertexBufferObject.glBufferDataARB(
					ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, data,
					ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
		}
	}

	public static void glBindBuffer(int ID) {
		if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
			ARBVertexBufferObject.glBindBufferARB(
					ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, ID);
		}
	}
	
	public static void glBindElementBuffer(int ID) {
		if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
			ARBVertexBufferObject.glBindBufferARB(
					ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, ID);
		}
	}
	
	public static void glDeleteBuffer(int ID) {
		ARBVertexBufferObject.glDeleteBuffersARB(ID);
	}
}
